<template>
  <el-card>
    <el-form ref="formRef" :inline="true" :model="formModel" label-width="80px">
      <template v-for="item in config.formItemList" :key="item.prop">
        <el-form-item :label="item.label" :prop="item.prop">
          <template v-if="item.type === 'select'">
            <el-select v-model="formModel[item.prop]" :placeholder="'请选择' + item.label">
              <el-option v-for="option in item.options" :key="option.value" :label="option.label"
                :value="option.value" />
            </el-select>
          </template>
          <template v-else>
            <el-input v-model="formModel[item.prop]" :placeholder="'请输入' + item.label" clearable />
          </template>
        </el-form-item>
      </template>
    </el-form>
    <div class="text-right">
      <el-button type="primary" icon="Search" @click="onSubmit">查询</el-button>
      <el-button icon="RefreshLeft" @click="onReset">重置</el-button>
    </div>
  </el-card>
</template>

<script lang="ts" setup>
import { ElForm } from 'element-plus';
import { reactive, ref } from 'vue'

interface PropParms {
  config: {
    formItemList: any[]
  }
}
const props = defineProps<PropParms>()

const initialFormModel: any = {}
for (const item of props.config.formItemList) {
  initialFormModel[item.prop] = item.initialValue ?? ""
}

const formModel = reactive(initialFormModel)

// 自定义事件
const emit = defineEmits(["on-submit", "on-reset"])

const onSubmit = () => {
  emit("on-submit", formModel)
}

const formRef = ref<InstanceType<typeof ElForm>>()
const onReset = () => {
  formRef.value?.resetFields()
  emit("on-submit", formModel)
}
</script>

<style lang="scss" scoped>
.el-input {
  --el-input-width: 220px;
}

.el-select {
  --el-select-width: 220px;
}
</style>
